package com.sentiment.bean;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;

import com.sentiment.database.bean.ChargeInfo;
import com.sentiment.database.bean.Role;
import com.sentiment.database.bean.User;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;

public class ChangeUserRole {
	
	
	/**
	 * 免费用户变付费用户
	 * 权限更改
	 */
	public void FreeToPay(){
		
		ChargeInfo chargeinfo = new ChargeInfo(null, null, null, Format.date2String(new Date(), 3), null, "付费用户", null);
		List<Document> tlist = new ArrayList<>();
		String []keys = {"startdate","item"};
		DbUtil db = new DbUtil();
		db.openConnection(ChargeInfo.dbName, ChargeInfo.collection);
		tlist = db.queryMany(chargeinfo.toDocument(), keys);
		db.closeConnection();
		
		if(tlist.size() != 0){
			Role role = new Role();
			role = role.getRoleByRoleName("pay");
			User user = new User();
			for (int i = 0; i < tlist.size(); i++) {
				chargeinfo = new ChargeInfo(tlist.get(i));
				user = user.getUserByUser_id(chargeinfo.getUser_id());
				String info =user.addRole(role);
				System.out.println(user.getUsercode()+"   "+info);
			}
			
		}
		
	}
	
	/**
	 * 付费用户变免费用户
	 * 权限更改
	 */
	public void PayToFree(){
		ChargeInfo chargeinfo = new ChargeInfo(null, null, null, null, Format.date2String(new Date(), 3), "付费用户", null);
		List<Document> tlist = new ArrayList<>();
		String []keys = {"enddate","item"};
		DbUtil db = new DbUtil();
		db.openConnection(ChargeInfo.dbName, ChargeInfo.collection);
		tlist = db.queryMany(chargeinfo.toDocument(), keys);
		db.closeConnection();
		
		if(tlist.size() != 0){
			Role role = new Role();
			role = role.getRoleByRoleName("pay");
			User user = new User();
			for (int i = 0; i < tlist.size(); i++) {
				chargeinfo = new ChargeInfo(tlist.get(i));
				user = user.getUserByUser_id(chargeinfo.getUser_id());
				String info = user.removeRole(role);
				System.out.println(user.getUsercode()+"   "+info);
			}
			
		}
	}
	
	public static void main(String[] args) {
		ChangeUserRole test = new ChangeUserRole();
		test.PayToFree();
	}
	
}
